//
//  FilterBrandChild.swift
//  koolcar
//
//  Created by xiantian on 2023/11/28.
//

import UIKit
import TTIndexBar

typealias FilterBrandListViewBlock = (KatashikiListData?)->Void
class FilterBrandListView: UIView {
    var selectBrandBlock: FilterBrandListViewBlock!
    var dataArray: [[String: [KatashikiListData]]] = [] {
        didSet {
            let indexArray = self.dataArray.flatMap{($0 as NSDictionary).allKeys} as! [String]
            self.indexBar.setIndexes(indexArray)
            self.tableView.reloadData()
        }
    }
    var tableView: BaseTableView!
    var indexBar: TTIndexBar!
    var indexArray: [String] = []
    var selectBrandBtn: UIButton!
    override init(frame: CGRect) {
        super.init(frame: frame)
        let tempHeader = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 40))
        selectBrandBtn = UIButton()
        selectBrandBtn.setup_titleColor(color: Theme.mainColor)
        selectBrandBtn.backgroundColor = UIColor.clear
        selectBrandBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        selectBrandBtn.contentHorizontalAlignment = .left
        selectBrandBtn.addTarget(self, action: #selector(selectBrandBtnAction), for: .touchUpInside)
        selectBrandBtn.setup_titleLabel(text: MMLocalizedString("all_brands", comment: "全部品牌"))
        tempHeader.addSubview(selectBrandBtn)
        tempHeader.buttomBorder(width: 0.5, borderColor: Theme.lineColor)
        self.addSubview(tempHeader)
        
        tableView = BaseTableView.init(frame: CGRect.zero, style: .plain)
        tableView.register(FilterBrandViewCell.self, forCellReuseIdentifier: "FilterBrandViewCell")
        tableView.register(FilterBrandCharHeaderCell.self, forHeaderFooterViewReuseIdentifier: "FilterBrandCharHeaderCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        self.addSubview(tableView)
        
        self.indexBar = TTIndexBar(frame: CGRect.zero)
        self.indexBar.delegate = self
        self.indexBar.textColor = UIColor(hex: 0x939AA8)
        self.indexBar.selectedTextColor = UIColor(hex: 0xDBC283)
        self.indexBar.detailViewDrawColor = UIColor(hex: 0xDBC283)
        self.indexBar.detailViewTextColor = .white
        self.indexBar.selectedBackgroundColor = .clear
        self.indexBar.sectionHeight = 20
        self.addSubview(self.indexBar)
        self.addSnpLayout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSnpLayout(){
        selectBrandBtn.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.bottom.equalTo(0)
            make.right.equalTo(-14)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(40)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        
        indexBar.snp.makeConstraints { make in
            make.right.equalTo(0)
            make.width.equalTo(20)
            make.top.equalTo(30)
            make.bottom.equalTo(0)
        }
    }
    
    @objc func selectBrandBtnAction(){
        self.selectBrandBlock?(nil)
    }
}

extension FilterBrandListView: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard self.dataArray.count > section else {
            return 0
        }
        let tempDict = self.dataArray[section] as NSDictionary
        let key = tempDict.allKeys.first ?? ""
        let rowItem = tempDict[key] as! [KatashikiListData]
        return rowItem.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 24
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "FilterBrandCharHeaderCell") as! FilterBrandCharHeaderCell
        if self.dataArray.count > section {
            let model = self.dataArray[section]
            header.titleLab.text = model.keys.first
        }
        header.backgroundColor = UIColor.red
        return header
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FilterBrandViewCell", for: indexPath) as! FilterBrandViewCell
        if self.dataArray.count > indexPath.section {
            let model = self.dataArray[indexPath.section] as NSDictionary
            let key = model.allKeys.first as! String
            let rowItem = model[key] as! [KatashikiListData]
            cell.titleLab.text = rowItem[indexPath.row].brandName ?? ""
        }
        cell.selectionStyle = .default
        cell.clipsToBounds = true
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if self.dataArray.count > indexPath.section {
            let model = self.dataArray[indexPath.section] as NSDictionary
            let key = model.allKeys.first as! String
            let rowItem = model[key] as! [KatashikiListData]
            let tempItem = rowItem[indexPath.row]
            self.selectBrandBlock?(tempItem)
            
        }
    }
}

extension FilterBrandListView: TTIndexBarDelegate {
    func indexDidChanged(_ indexBar: TTIndexBar!, index: Int, title: String!) {
        self.tableView.scrollToRow(at: IndexPath.init(row: 0, section: index), at: .top, animated: false)
    }
}
